package com.sanyang.pda.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.device.ScanManager;
import android.device.scanner.configuration.PropertyID;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.sanyang.pda.bean.ScanBean;


public class ScanButView extends androidx.appcompat.widget.AppCompatButton implements View.OnLongClickListener {
    private final static String SCAN_ACTION = ScanManager.ACTION_DECODE;//default action

    private Vibrator mVibrator;
    private ScanManager mScanManager;
    private SoundPool soundpool = null;
    private int soundid;
    private String barcodeStr;
    private boolean isScaning = false;


    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            isScaning = false;
            soundpool.play(soundid, 1, 1, 0, 0, 1);
            mVibrator.vibrate(100);
            byte[] barcode = intent.getByteArrayExtra(ScanManager.DECODE_DATA_TAG);
            int barcodelen = intent.getIntExtra(ScanManager.BARCODE_LENGTH_TAG, 0);
            byte temp = intent.getByteExtra(ScanManager.BARCODE_TYPE_TAG, (byte) 0);
            android.util.Log.i("debug", "----codetype--" + temp);
            ScanBean scanBean = null;
            try {
                barcodeStr = new String(barcode, 0, barcodelen);
                scanBean = new Gson().fromJson(barcodeStr, ScanBean.class);
            } catch (Exception e) {
                e.printStackTrace();
                //Toast.makeText(context, "请扫正确的二维码", Toast.LENGTH_SHORT).show();
                //return;
                scanBean=new ScanBean(barcodeStr,-1);
            }

            //showScanResult.setText(barcodeStr);
            if (scanCallBack != null) {
                scanCallBack.callBack(scanBean);
            }
        }

    };

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public ScanButView(Context context) {
        this(context,null);
    }

    public ScanButView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ScanButView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //init();
    }

   /* private void init() {
        setOnLongClickListener(this);
        mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
        initScan();
        //showScanResult.setText("");
        IntentFilter filter = new IntentFilter();
        int[] idbuf = new int[]{PropertyID.WEDGE_INTENT_ACTION_NAME, PropertyID.WEDGE_INTENT_DATA_STRING_TAG};
        String[] value_buf = mScanManager.getParameterString(idbuf);
        if(value_buf != null && value_buf[0] != null && !value_buf[0].equals("")) {
            filter.addAction(value_buf[0]);
        } else {
            filter.addAction(SCAN_ACTION);
        }

        getContext().registerReceiver(mScanReceiver, filter);
    }*/

 /*   private void initScan() {
        // TODO Auto-generated method stub
        mScanManager = new ScanManager();
        mScanManager.openScanner();

        mScanManager.switchOutputMode( 0);
        soundpool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 100); // MODE_RINGTONE
        soundid = soundpool.load("/etc/Scan_new.ogg", 1);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(mScanManager != null) {
            mScanManager.stopDecode();
            isScaning = false;
        }
        try {
            getContext().unregisterReceiver(mScanReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            stopScan();
        } else if (event.getAction() == MotionEvent.ACTION_DOWN){
            startScan();
        }
        return super.onTouchEvent(event);
    }

    private void stopScan() {
        mScanManager.stopDecode();
    }

    @Override
    public boolean onLongClick(View v) {
        //startScan();
        return false;
    }

    private void startScan() {
        // TODO Auto-generated method stub
        //if(type == 3)
        mScanManager.stopDecode();
        isScaning = true;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mScanManager.startDecode();
    }
*/
    public void setScanCallBack(ScanCallBack scanCallBack) {
        this.scanCallBack = scanCallBack;
    }

    private ScanCallBack scanCallBack ;

    @Override
    public boolean onLongClick(View v) {
        return false;
    }


    public interface ScanCallBack{
        void callBack(ScanBean scanBean);
    }
}
